import type { Schedule, Teacher, Classroom, Student } from '../SchedulingManagementPage';

import React from 'react';

export interface Conflict {
  id: string;
  type: string;
  schedules: string[];
  detail: string;
  solution: string;
}

export interface Teacher {
  id: string;
  name: string;
  availableTimes?: string[];
  unavailableTimes?: string[];
}

export interface Classroom {
  id: string;
  name: string;
  capacity: number;
  availableTimes?: string[];
  unavailableTimes?: string[];
}

export interface Student {
  id: string;
  name: string;
  className: string;
  availableTimes?: string[];
  unavailableTimes?: string[];
}

export interface Schedule {
  id: string;
  courseId: string;
  teacherId: string;
  classroomId: string;
  startTime: string;
  endTime: string;
  studentIds: string[];
}

export class ScheduleConflictDetector {
  private schedules: Schedule[];
  private teachers: Teacher[];
  private classrooms: Classroom[];
  private students: Student[];

  constructor(schedules: Schedule[], teachers: Teacher[], classrooms: Classroom[], students: Student[]) {
    this.schedules = schedules;
    this.teachers = teachers;
    this.classrooms = classrooms;
    this.students = students;
  }

  // 检测所有冲突
  detectConflicts(): Conflict[] {
    const conflicts: Conflict[] = [];
    
    // 检测教师时间冲突
    conflicts.push(...this.detectTeacherConflicts());
    
    // 检测教室冲突
    conflicts.push(...this.detectClassroomConflicts());
    
    // 检测学生时间冲突
    conflicts.push(...this.detectStudentConflicts());
    
    // 检测其他类型的冲突
    conflicts.push(...this.detectOtherConflicts());
    
    return conflicts;
  }

  // 检测教师时间冲突
  private detectTeacherConflicts(): Conflict[] {
    const conflicts: Conflict[] = [];
    const teacherSchedulesMap = new Map<string, Schedule[]>();
    
    // 按教师分组排课
    this.schedules.forEach(schedule => {
      if (!teacherSchedulesMap.has(schedule.teacherId)) {
        teacherSchedulesMap.set(schedule.teacherId, []);
      }
      teacherSchedulesMap.get(schedule.teacherId)!.push(schedule);
    });
    
    // 检测每个教师的排课是否有时间冲突
    teacherSchedulesMap.forEach((teacherSchedules, teacherId) => {
      for (let i = 0; i < teacherSchedules.length; i++) {
        for (let j = i + 1; j < teacherSchedules.length; j++) {
          const schedule1 = teacherSchedules[i];
          const schedule2 = teacherSchedules[j];
          
          if (this.isTimeOverlap(new Date(schedule1.startTime), new Date(schedule1.endTime), new Date(schedule2.startTime), new Date(schedule2.endTime))) {
            conflicts.push({
              id: `teacher-conflict-${Date.now()}-${i}-${j}`,
              type: '教师时间冲突',
              schedules: [schedule1.id, schedule2.id],
              detail: `教师 ${this.teachers.find(t => t.id === teacherId)?.name || teacherId} 在 ${new Date(schedule1.startTime).toLocaleString()} 同时有两个课程`,
              solution: '调整其中一个课程的时间或更换教师',
            });
          }
        }
      }
    });
    
    return conflicts;
  }

  // 检测教室冲突
  private detectClassroomConflicts(): Conflict[] {
    const conflicts: Conflict[] = [];
    const classroomSchedulesMap = new Map<string, Schedule[]>();
    
    // 按教室分组排课
    this.schedules.forEach(schedule => {
      if (!classroomSchedulesMap.has(schedule.classroomId)) {
        classroomSchedulesMap.set(schedule.classroomId, []);
      }
      classroomSchedulesMap.get(schedule.classroomId)!.push(schedule);
    });
    
    // 检测每个教室的排课是否有时间冲突
    classroomSchedulesMap.forEach((classroomSchedules, classroomId) => {
      for (let i = 0; i < classroomSchedules.length; i++) {
        for (let j = i + 1; j < classroomSchedules.length; j++) {
          const schedule1 = classroomSchedules[i];
          const schedule2 = classroomSchedules[j];
          
          if (this.isTimeOverlap(new Date(schedule1.startTime), new Date(schedule1.endTime), new Date(schedule2.startTime), new Date(schedule2.endTime))) {
            conflicts.push({
              id: `classroom-conflict-${Date.now()}-${i}-${j}`,
              type: '教室冲突',
              schedules: [schedule1.id, schedule2.id],
              detail: `教室 ${this.classrooms.find(c => c.id === classroomId)?.name || classroomId} 在 ${new Date(schedule1.startTime).toLocaleString()} 同时被安排了两个课程`,
              solution: '调整其中一个课程的时间或更换教室',
            });
          }
        }
      }
    });
    
    return conflicts;
  }

  // 检测学生时间冲突
  private detectStudentConflicts(): Conflict[] {
    const conflicts: Conflict[] = [];
    
    // 为每个学生检查时间冲突
    this.students.forEach(student => {
      const studentSchedules = this.schedules.filter(schedule => 
        schedule.studentIds.includes(student.id)
      );
      
      for (let i = 0; i < studentSchedules.length; i++) {
        for (let j = i + 1; j < studentSchedules.length; j++) {
          const schedule1 = studentSchedules[i];
          const schedule2 = studentSchedules[j];
          
          if (this.isTimeOverlap(new Date(schedule1.startTime), new Date(schedule1.endTime), new Date(schedule2.startTime), new Date(schedule2.endTime))) {
            conflicts.push({
              id: `student-conflict-${Date.now()}-${student.id}-${i}-${j}`,
              type: '学生时间冲突',
              schedules: [schedule1.id, schedule2.id],
              detail: `学生 ${student.name} 在 ${new Date(schedule1.startTime).toLocaleString()} 同时有两个课程`,
              solution: '调整其中一个课程的时间或允许学生选择其中一个课程',
            });
          }
        }
      }
    });
    
    return conflicts;
  }

  // 检测其他类型的冲突
  private detectOtherConflicts(): Conflict[] {
    const conflicts: Conflict[] = [];
    
    // 检查课程所需的设备是否与教室提供的设备匹配
    // 实际项目中应根据具体需求实现更多的冲突检测逻辑
    
    return conflicts;
  }

  // 检查时间是否重叠
  private isTimeOverlap(start1: Date, end1: Date, start2: Date, end2: Date): boolean {
    return start1 < end2 && start2 < end1;
  }
}

export default ScheduleConflictDetector;